#include "ItemStack.h"
#include "ItemDatabase.h"

ItemStack::ItemStack()
: stack(0)
{
}

ItemStack::ItemStack(const ItemPtr& item, size_t stack, size_t maxStack)
: item(item)
, stack(stack)
, maxStack(maxStack)
, icon(new AnimatedSprite(*ItemDatabase::get().getData(*item).getIcon()))
{
}

ItemStack::~ItemStack()
{
}

ItemStack& ItemStack::operator=(const ItemStack& rhs)
{
    if(this == &rhs) return *this;

    item = rhs.item;
    stack = rhs.stack;

    return *this;
}

bool operator==(const ItemStack& lhs, const ItemStack& rhs)
{
	return &lhs == &rhs || *(lhs.item) == *(rhs.item) && lhs.stack == rhs.stack;
}

bool operator<(const ItemStack& lhs, const ItemStack& rhs)
{
    if(*(lhs.item) == *(rhs.item))
    { // Identical items; test for lesser quantity.
        return lhs.stack < rhs.stack;
    }

    return lhs.item < rhs.item;
}

ItemPtr ItemStack::getItem() const
{
    return item;
}

size_t ItemStack::getStack() const
{
    return stack;
}

void ItemStack::setStack( size_t stack )
{
    this->stack = stack;
}

int ItemStack::addItems( size_t numberToAdd )
{
    if (numberToAdd + stack > maxStack)
    {
        int tmpStackAdded = maxStack - stack;
        stack = maxStack;
        return numberToAdd - tmpStackAdded;
    }
    else
    {
        stack += numberToAdd;
        return 0;
    }

}

size_t ItemStack::getMaxStack()
{
    return maxStack;
}

AnimatedSpritePtr ItemStack::getIcon() const
{
    return icon;
}
